package com.example.canal.common.util;

import com.example.canal.common.bean.CanalBean;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/8/8 18:40
 */
public class DbChangeExecutor {

    /**
     *
     */
    public static final ConcurrentHashMap<String, ThreadPoolExecutor> dbChangeExecutorMap = new ConcurrentHashMap<>(32);

    /**
     * 根据tableName 获取线程池 每个tableName 一个线程池
     *
     * @param canalBean
     * @return
     */
    public static Executor getExecutorByTableName(CanalBean canalBean) {

        return dbChangeExecutorMap.computeIfAbsent(canalBean.getTable(),DbChangeExecutor::generateThreadPoolExecutor);
    }


    private static ThreadPoolExecutor generateThreadPoolExecutor(String tableName) {
        return new ThreadPoolExecutor(1,1,0L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<>());
    }


}
